package cc.rengu.redp.bizimpl.igas.service.impl;

import cc.rengu.redp.bizimpl.igas.dao.PDstChannelCfgMapper;
import cc.rengu.redp.bizimpl.igas.domain.PDstChannelCfg;
import cc.rengu.redp.bizimpl.igas.service.PDstChannelCfgService;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpPrivilegeCheckService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.RedpUtil;
import cc.rengu.redp.common.utils.SortUtil;
import cc.rengu.redp.system.domain.User;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-09
 */
@DS("igas")
@Service
public class PDstChannelCfgServiceImpl extends ServiceImpl<PDstChannelCfgMapper, PDstChannelCfg> implements PDstChannelCfgService {
    @Autowired
    private IUpmpPrivilegeCheckService privilegeCheckService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    private String oprMenuId = "20000014";
    private String oprMenuName = "产品管理:支付通道管理";
    private String oprBtnNamePrefix = "支付通道管理";
    private String webNextUrl = "/admin/igas/pSrcChannelInfo/edit";

    @Override
    public IPage<PDstChannelCfg> findByPage(QueryRequest request, PDstChannelCfg info) {
        try {
            LambdaQueryWrapper<PDstChannelCfg> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getInstId())) {
                queryWrapper.eq(PDstChannelCfg::getInstId, info.getInstId());
            } else {
                //获取当前用户的法人id
                UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
                queryWrapper.eq(PDstChannelCfg::getInstId, user.getInstId());
            }
            if (StringUtils.isNotBlank(info.getDstChannelId())) {
                queryWrapper.likeRight(PDstChannelCfg::getDstChannelId, info.getDstChannelId());
            }
            if (StringUtils.isNotBlank(info.getDstChannelType())) {
                queryWrapper.likeRight(PDstChannelCfg::getDstChannelType, info.getDstChannelType());
            }
            queryWrapper.orderByDesc(PDstChannelCfg::getDstChannelType);
            Page<PDstChannelCfg> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
    public List<PDstChannelCfg> findPDstChannelCfgList(String instId, String dstChannelId, String dstChannelType){
        List<PDstChannelCfg> list;
        LambdaQueryWrapper<PDstChannelCfg> queryWrapper = new LambdaQueryWrapper<>();
        if (Common.isBlank(instId)) {
            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            if (Common.isNotBlank(user) && !Common.isBlank(user.getInstId())) {
                queryWrapper.eq(PDstChannelCfg::getInstId, user.getInstId());
            }
        }else {
            queryWrapper.eq(PDstChannelCfg::getInstId, instId);
        }
        if(!Common.isBlank(dstChannelId)){
            queryWrapper.eq(PDstChannelCfg::getDstChannelId, dstChannelId);
        }
        if(!Common.isBlank(dstChannelType)){
            queryWrapper.eq(PDstChannelCfg::getDstChannelType, dstChannelType);
        }
       return this.list(queryWrapper);

    }
    @Override
    @Transactional
    public void update(PDstChannelCfg entity) throws RedpException {
        String lastUpddt = DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        entity.setUpdateTime(lastUpddt);
        LambdaQueryWrapper<PDstChannelCfg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PDstChannelCfg::getInstId, entity.getInstId());
        wrapper.eq(PDstChannelCfg::getDstChannelId, entity.getDstChannelId());
        wrapper.eq(PDstChannelCfg::getDstChannelType, entity.getDstChannelType());
        PDstChannelCfg pDstChannelCfg = this.getOne(wrapper);
        if (Common.isNotBlank(pDstChannelCfg)) {
            pDstChannelCfg.setDstChannelTypeDesc(entity.getDstChannelTypeDesc());
            pDstChannelCfg.setChannelNo(entity.getChannelNo());
            pDstChannelCfg.setProviderMchntNo(entity.getProviderMchntNo());
            pDstChannelCfg.setAppId(entity.getAppId());
            pDstChannelCfg.setSubAppId(entity.getSubAppId());
            this.update(pDstChannelCfg, wrapper);
        } else {
            this.save(entity);
        }
    }

    @Override
    public String cupsMchntName(String key, String value, String instId) {
        LambdaQueryWrapper<PDstChannelCfg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PDstChannelCfg::getInstId, instId);
        wrapper.eq(PDstChannelCfg::getDstChannelId, "UPAC");
        wrapper.eq(PDstChannelCfg::getDstChannelType, key);
        wrapper.eq(PDstChannelCfg::getProviderMchntNo, value);
        PDstChannelCfg pDstChannelCfg = this.getOne(wrapper);
        if (Common.isNotBlank(pDstChannelCfg)) {
            return pDstChannelCfg.getDstChannelTypeDesc();
        }
        return null;
    }

    @Override
    public List<Map<String, String>> select2List(String q) {
        q = q.replaceAll("&amp;", "&");

        String dstChannelType = "";
        String[] param = q.split("&");
        if (null != param && param.length > 0) {
            dstChannelType = param[0];
        }

        String instId = "";
        if (null != param && param.length > 1) {
            instId = param[1];
        }
        if (Common.isBlank(instId)) {
            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            instId = user.getInstId();
        }
        String dstChannelId = "";
        if (null != param && param.length > 2) {
            dstChannelId = param[2];
        }
        LambdaQueryWrapper<PDstChannelCfg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PDstChannelCfg::getInstId, instId);
        if (Common.isNotBlank(dstChannelId))
            wrapper.eq(PDstChannelCfg::getDstChannelId,dstChannelId);
        if (Common.isNotBlank(dstChannelId))
            wrapper.likeRight(PDstChannelCfg::getDstChannelType, dstChannelType);
        List<PDstChannelCfg> pDstChannelCfgList = this.list(wrapper);
        Map<String, String> map;
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        if (CollectionUtils.isNotEmpty(pDstChannelCfgList)) {
            for (int i = 0; i < pDstChannelCfgList.size(); i++) {
                PDstChannelCfg pDstChannelCfg = pDstChannelCfgList.get(i);
                map = new HashMap();
                map.put("id", pDstChannelCfg.getDstChannelType());
                map.put("name", pDstChannelCfg.getDstChannelTypeDesc());
                list.add(map);
            }
            list.stream().distinct().collect(Collectors.toList());
        }
        return list;
    }
   /* @Override
    @Transactional
    public void editFromChnl(PDstChannelInfo pDstChannelInfo) throws RedpException {
        String instId="001";
        // 获取当前登录用户信息
//        User currentUser = RedpUtil.getCurrentUser();
        User user = new User();
        user.setId("1");
        if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(pDstChannelInfo.getOptFlag())) {
            log.error("不支持审核操作!");
            throw new RedpException("不支持审核操作！");
        }else if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(pDstChannelInfo.getOptFlag())
                ||PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(pDstChannelInfo.getOptFlag())){
            if(Common.isNotBlank(pDstChannelInfo.getSecPlanId())
                    &&pDstChannelInfo.getSecPlanId().startsWith(pDstChannelInfo.getDstChannelId())){//secPlanId中的DstChannelId未变时，secPlanId不变
            }else{
                pDstChannelInfo.setSecPlanId(genSecPlanId(pDstChannelInfo.getDstChannelId()));
            }

            pDstChannelInfoService.saveData( pDstChannelInfo, user, org, authPrivilegeCheckService);
        }
        String lastUpddt = DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        entity.setUpdateTime(lastUpddt);
        LambdaQueryWrapper<PDstChannelCfg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PDstChannelCfg::getInstId, entity.getInstId());
        wrapper.eq(PDstChannelCfg::getDstChannelId, entity.getDstChannelId());
        wrapper.eq(PDstChannelCfg::getDstChannelType, entity.getDstChannelType());
         PDstChannelCfg pDstChannelCfg = this.getOne(wrapper);
        if (Common.isNotBlank(pDstChannelCfg)) {
            pDstChannelCfg.setDstChannelTypeDesc(entity.getDstChannelTypeDesc());
            pDstChannelCfg.setChannelNo(entity.getChannelNo());
            pDstChannelCfg.setProviderMchntNo(entity.getProviderMchntNo());
            pDstChannelCfg.setAppId(entity.getAppId());
            pDstChannelCfg.setSubAppId(entity.getSubAppId());
           this.update(pDstChannelCfg,wrapper);
        } else {
           this.save(entity);
        }
    }*/

    /* *//**
     * 安全计划编号，格式为: dstChannelId+8位排序编码
     * @return
     *//*
    private String genSecPlanId(String dstChannelId) {
        // 固定入参
        String common = "SEC_PLAN_ID";
        String prefix = dstChannelId;

//		dstChannelId+8位排序编码
        // 生成序列号
        String serialNumber = upmpSysParamService.updateParamValue(common, prefix, "20");
        String result = prefix + String.format("%08d", Integer.parseInt(serialNumber));
        return  result;
    }*/
}
